package com.example.oasystem.controller.attend;

import com.example.oasystem.dao.attend.AoaAttendsListDao;
import com.example.oasystem.dao.login.AoaUserDao;
import com.example.oasystem.entity.attend.AoaAttendsList;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.type.AoaTypeList;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaAttendsListService;
import com.example.oasystem.service.AoaStatusListService;
import com.example.oasystem.service.AoaTypeListService;
import com.example.oasystem.service.AoaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 考勤报表
 * @author 10440
 */
@Controller
public class AttendWeekController {
    String sousuo = null;
    String oldstarttime;
    String oldendtime;
    @Autowired
    private AoaUserService aoaUserService;
    @Autowired
    private AoaStatusListService aoaStatusListService;
    @Autowired
    private AoaAttendsListDao aoaAttendsListDao;
    @Autowired
    private AoaTypeListService aoaTypeListService;
    @Autowired
    private AoaAttendsListService aoaAttendsListService;
    @Autowired
    private AoaUserDao aoaUserDao;

    @RequestMapping("/attendceweek")
    public String toWeekTable(Model model){
        Page page = new Page(1,0,0,0,false,false);
        model.addAttribute("page",page);
        model.addAttribute("url","searchrealweektable");
        return "attendce/weektable";
    }

    @RequestMapping("/realweektable")
    public String realweektable(Model model, HttpSession session,
                                @RequestParam String starttime,
                                @RequestParam String endtime,
                                @RequestParam(value = "baseKey",required = false,defaultValue = "") String baseKey){
        List<String> weekday = new ArrayList<>();
        Collections.addAll(weekday,"星期一","星期二","星期三","星期四","星期五","星期六","星期日");

        AoaUser user = (AoaUser)session.getAttribute("user");
        List<AoaUser> aoaUsers = aoaUserService.selectUserDeptAndAttendsList(user.getUserId(),starttime,endtime,baseKey,0,0);

        oldstarttime = starttime;
        oldendtime = endtime;
        sousuo = baseKey;
        int totalElement= aoaUsers.size();
        int size=3;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        int number=0;
        boolean last;
        if (number==(totalPages-1) || totalPages == 0) {
            last=true;
        } else {
            last=false;
        }
        Page page=new Page(totalElement,size,totalPages,number, true,last);
        List<AoaUser> ulist = aoaUserService.selectUserDeptAndAttendsList(user.getUserId(),starttime,endtime,baseKey,0,size);
        List<AoaUser> xiaShulist = aoaUserDao.selectAllXiashu(user.getUserId());
        if (ulist.isEmpty()){
            ulist.addAll(xiaShulist);
        }

        model.addAttribute("weekday",weekday);
        model.addAttribute("ulist",ulist);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("page",page);
        model.addAttribute("url","searchrealweektable");
        return "attendce/realweektable";
    }

    @RequestMapping("/searchrealweektable")
    public String searchrealweektable(@RequestParam(value = "page",required = false,defaultValue = "0") int page,
                                      Model model,
                                      HttpServletRequest request, HttpSession session,
                                      @RequestParam(value = "starttime",required = false,defaultValue = "") String starttime,
                                      @RequestParam(value = "endtime",required = false,defaultValue = "") String endtime,
                                      @RequestParam(value = "baseKey",required = false,defaultValue = "")String baseKey){
        if(starttime.equals("")&&endtime.equals("")){
            starttime = oldstarttime;
            endtime = oldendtime;
        }
        if(baseKey.equals("")){
            baseKey = sousuo;
        }
        AoaUser user = (AoaUser)session.getAttribute("user");
        //System.out.println("user = " + user);
        //System.out.println("sousuo = " + sousuo);
        List<AoaUser> aoaUsers = aoaUserService.selectUserDeptAndAttendsList(user.getUserId(),starttime,endtime,baseKey,0,0);
        //System.out.println("aoaUsers = " + aoaUsers);
        int totalElement= aoaUsers.size();
        int size=3;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        int number=page;
        boolean first;
        if (number==0) {
            first=true;
        } else {
            first=false;
        }
        boolean last;
        if (number==(totalPages-1) || totalPages == 0) {
            last=true;
        } else {
            last=false;
        }
        Page fenye = new Page(totalElement,size,totalPages,number, first,last);
        List<AoaUser> ulist = aoaUserService.selectUserDeptAndAttendsList(user.getUserId(),starttime,endtime,baseKey,number*size,size);
        List<AoaUser> xiaShulist = aoaUserDao.selectAllXiashu(user.getUserId());
        if (ulist.isEmpty()){
            ulist.addAll(xiaShulist);
        }

        List<String> weekday = new ArrayList<>();
        Collections.addAll(weekday,"星期一","星期二","星期三","星期四","星期五","星期六","星期日");
        model.addAttribute("ulist",ulist);
        model.addAttribute("weekday",weekday);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("page",fenye);
        model.addAttribute("url","searchrealweektable");
        return "attendce/realweektable";
    }

    @RequestMapping("/attendceedit")
    public String attendceedit(Model model,@RequestParam String aid){
        List<AoaStatusList> statusLists = aoaStatusListService.selectStatusListByModel();
        AoaAttendsList attends = aoaAttendsListDao.selectByIdOnChen(aid);
        List<AoaTypeList> typeLists = aoaTypeListService.selectTypeListByModel();

        model.addAttribute("attends",attends);
        System.out.println("attends = " + attends);
        model.addAttribute("statuslist",statusLists);
        model.addAttribute("typelist",typeLists);
        return "attendce/attendceedit";
    }

    @RequestMapping("/attendcesave")
    public String saveAttendce(@RequestParam String attendsRemark,
                               @RequestParam String statusId,
                               @RequestParam String attendsId){
        aoaAttendsListService.updateStatusIdAndRemarkById(attendsRemark,Long.parseLong(statusId),Long.parseLong(attendsId));
        return "forward:/attendceweek";
    }
}
